package com.example.springboothomework.controller;

import com.example.springboothomework.common.*;
import com.example.springboothomework.entity.dto.EmployeeDTO;
import com.example.springboothomework.entity.Employee;
import com.example.springboothomework.entity.query.EmployeeExcelExportQuery;
import com.example.springboothomework.entity.query.EmployeeQuery;
import com.example.springboothomework.exception.BusinessException;
import com.example.springboothomework.exception.ResultCodeEnum;
import com.example.springboothomework.service.IEmployeeService;
import com.example.springboothomework.validator.ValidGroup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @author: chenjianhao
 * @date: 2025/6/5
 * @description: 员工控制器
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@RequiredArgsConstructor
@Validated
public class EmployeeController {

    private final IEmployeeService employeeService;

    /**
     * 添加员工
     *
     * @param employeeDTO 员工dto
     * @return
     * @author: chenjianhao
     * @date: 2025/6/5
     */
    @PostMapping("/save")
    public BaseResponse<Long> addEmployee(@Validated(ValidGroup.Save.class) @RequestBody EmployeeDTO employeeDTO) {
        log.info("添加员工: {}", employeeDTO);
        Long employeeId = employeeService.save(employeeDTO);
        return ResultUtils.success(employeeId);
    }

    /**
     * 删除员工
     *
     * @param id id
     * @return
     * @author: chenjianhao
     * @date: 2025/6/5
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Void> delete(@NotNull @Min(1L) @PathVariable Long id) {
        log.info("删除员工: id={}", id);
        employeeService.delete(id);
        return ResultUtils.success();
    }

    /**
     * 更新员工
     *
     * @param employeeDTO 员工dto
     * @return
     * @author: chenjianhao
     * @date: 2025/6/5
     */
    @PostMapping("/update")
    public BaseResponse<Void> update(@Validated(ValidGroup.Update.class) @RequestBody EmployeeDTO employeeDTO) {
        log.info("更新员工: {}", employeeDTO);
        employeeService.update(employeeDTO);
        return ResultUtils.success();
    }

    /**
     * 根据id获取员工信息
     *
     * @param id id
     * @return
     * @author: chenjianhao
     * @date: 2025/6/5
     */
    @GetMapping("/info/{id}")
    public BaseResponse<Employee> getEmployeeById(@NotNull @Min(1L) @PathVariable Long id) {
        log.info("获取员工: id={}", id);
        return ResultUtils.success(employeeService.info(id));
    }

    /**
     * 分页查询员工列表
     *
     * @param employeeQuery 员工查询参数
     * @return
     * @author: chenjianhao
     * @date: 2025/6/5
     */
    @PostMapping("/query-page")
    public BaseResponse<PageResponse<Employee>> queryPage(@Valid @RequestBody EmployeeQuery employeeQuery) {
        Integer pageNum = employeeQuery.getPageNum();
        Integer pageSize = employeeQuery.getPageSize();
        String searchKeyword = employeeQuery.getSearchKeyword();
        Long companyId = employeeQuery.getCompanyId();
        log.info("查询员工列表: page={}, size={}, keyword={}, companyId={}", pageNum, pageSize, searchKeyword, companyId);
        return ResultUtils.success(employeeService.queryPage(pageNum, pageSize, searchKeyword, companyId));
    }

    /**
     * 导出员工
     *
     * @param employeeExcelExportQuery 员工导出查询参数
     * @param response
     * @return
     * @author: chenjianhao
     * @date: 2025/6/9
     */
    @PostMapping("/export")
    public void exportEmployees(@RequestBody EmployeeExcelExportQuery employeeExcelExportQuery, HttpServletResponse response) {
        log.info("导出员工列表");
        employeeService.exportEmployees(employeeExcelExportQuery,response);
    }

    /**
     * 导入员工
     *
     * @param file  文件
     * @return
     * @author: chenjianhao
     * @date: 2025/6/10
     */
    @PostMapping("/import")
    public BaseResponse<Void> importEmployees(MultipartFile file,HttpServletResponse response) {
        log.info("导入员工列表");
        if (file == null || file.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.FILE_EMPTY);
        }
        //校验后缀是否为xlsx
        if (!file.getOriginalFilename().endsWith(".xlsx")) {
            throw new BusinessException(ResultCodeEnum.FILE_FORMAT_ERROR);
        }
        ImportResult importResult = employeeService.importEmployees(file);
        List<ImportError> errors = importResult.getErrors();
        //若有失败案例
        if (errors != null && !errors.isEmpty()) {
            employeeService.exportErrorData(errors,response);
            return null;
        }
        return ResultUtils.success();
    }
}